import 'package:get/get.dart';
import 'package:xin_he/page/mine/xin_he_mine_orders/xin_he_mine_order_detail/xin_he_new_order_detail_entity.dart';
import 'package:xin_he/utils/xin_he_UI_constancs.dart';

import '../../../../service/xin_he_reqest.dart';
import '../../../../utils/xin_he_storage.dart';
import '../../../../utils/xin_he_toast.dart';
import '../../../shopping_car/xin_he_instant_pay/xin_he_instant_pay_view.dart';
import 'xin_he_mine_order_detail_state.dart';

class XinHeMineOrderDetailLogic extends GetxController {
  final XinHeMineOrderDetailState state = XinHeMineOrderDetailState();
  Map orderMap = {};
  Rx<XinHeNewOrderDetailEntity> model = XinHeNewOrderDetailEntity().obs;
  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    orderMap = Get.arguments;
  }

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();

    queryOrderStatus();

  }

  queryOrderStatus() async {


    var userId = await XinHeLocalStorage.get(XinHeStorage.userId);
    Map<String,dynamic> param = {
      'orderId': orderMap['orderId']
    };
    XinHeRequestDataResult result = await XinHeHttpRequestData.requestUniversalPost(url: '/order/detailOrder',params: param,isPost: false);

    if(!result.result){
      XinHeToast.center(result.data['message']);
      return ;
    }

    if(result.data['code'] == 0 ){
        XinHeNewOrderDetailEntity entity = XinHeNewOrderDetailEntity.fromJson(result.data['data']);
        model.value = entity;

    }

  }

  getRightButtonTitle(String status) {
    String title = '';
    if(status == 'UN_PAY') {
      title = '去付款';
    }
    if(status == 'PAYED') {
      title = '提醒发货';
    }
    if(status == 'SENDED') {
      title = '确认发货';
    }
    if(status == 'RECEIVED') {
      title = '去评价';
    }
    return title;
  }

  getLeftButtonTitle(String status) {
    String title = '';

    if(status == 'UN_PAY') {
      title = '取消订单';
    }
    if(status == 'PAYED') {
      title = '取消订单';
    }
    if(status == 'SENDED') {
      title = '确认发货';
    }
    if(status == 'RECEIVED') {
      title = '去评价';
    }
    return title;
  }

  /// 有订单的支付
  toPayAction() async {
    Get.bottomSheet(XinHeInstantPayPage(
      zoneType: model.value.zoneType!,
      totalPrice: model.value.orderFee!,
      count: model.value.orderAmount!,
      order: {'orderId': model.value.id!},
      scene: PayScene.fromGoodsOrdersListItemDetail,),
      persistent: false,isScrollControlled: true,);

  }

  ///取消订单
  toCancelAction() async {
    var userId = await XinHeLocalStorage.get(XinHeStorage.userId);
    Map<String,dynamic> param = {
      'orderId': orderMap['orderId']
    };
    XinHeRequestDataResult result = await XinHeHttpRequestData.requestUniversalPost(url: '/order/cancelOrder',params: param,isPostAndwwwForm: true);

    if(!result.result){
      XinHeToast.center(result.data['message']);
      return ;
    }

    if(result.data['code'] == 0 ){


    }
  }

  ///提醒发货
  toRemindSendAction() async {
    var userId = await XinHeLocalStorage.get(XinHeStorage.userId);
    Map<String,dynamic> param = {
      'orderId': orderMap['orderId']
    };
    XinHeRequestDataResult result = await XinHeHttpRequestData.requestUniversalPost(url: '/order/saveRemindSend',params: param,isPostAndwwwForm: true);

    if(!result.result){
      XinHeToast.center(result.data['message']);
      return ;
    }

    if(result.data['code'] == 0 ){
      XinHeToast.center('提醒成功');


    }else {
      XinHeToast.center(result.data['message']);

    }
  }

  rightButtionAction() {
    String status = model.value.status!;
    if(status == 'UN_PAY') {
      toPayAction();
    }
    if(status == 'PAYED') {
      toRemindSendAction();
    }
    if(status == 'SENDED') {

    }
    if(status == 'RECEIVED') {

    }
  }
}
